package com.ztsoft.user.service.dynamic.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.dynamic.QueryInfoMapper;
import com.ztsoft.user.pojo.constant.UserConstant;
import com.ztsoft.user.pojo.entity.dynamic.QueryDetail;
import com.ztsoft.user.pojo.entity.dynamic.QueryInfo;
import com.ztsoft.user.pojo.entity.user.SysUser;
import com.ztsoft.user.service.dynamic.QueryDetailService;
import com.ztsoft.user.service.dynamic.QueryInfoService;
import com.ztsoft.user.service.user.SysUserService;
import com.ztsoft.user.utils.JWTUtils;
import com.ztsoft.user.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
public class QueryInfoServiceImpl extends ServiceImpl<QueryInfoMapper, QueryInfo> implements QueryInfoService {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private QueryDetailService queryDetailService;

    @Override
    public void saveQueryInfo(QueryInfo queryInfo) {
        UserHolder userInfo = sysUserService.getUserInfo();
        SysUser sysUser = userInfo.getSysUser();

        Date date = new Date(System.currentTimeMillis());
        queryInfo.setCreateId(sysUser.getUserId());
        queryInfo.setCreateTime(date);
        queryInfo.setId(UUID.randomUUID().toString().replace("-", ""));
        this.save(queryInfo);

        List<QueryDetail> advancedSearch = queryInfo.getMultiConditionList();
        for (QueryDetail queryDetail : advancedSearch) {
            queryDetail.setId(UUID.randomUUID().toString().replace("-", ""));
            queryDetail.setQueryId(queryInfo.getId());
            queryDetail.setCreateId(sysUser.getUserId());
            queryDetail.setCreateTime(date);
            queryDetailService.save(queryDetail);
        }
    }

    @Override
    public List<QueryInfo> getQueryInfoList(QueryInfo queryInfo) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request;
        SysUser sysUser = new SysUser();
        if (Objects.nonNull(attributes)) {
            request = attributes.getRequest();
            // 从head中获取token
            String token = request.getHeader(UserConstant.ACCESS_TOKEN);
            // 从token中解析userName
            String account = JWTUtils.getTokenInfo(token, "account");

            //获取用户信息
            QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", account);
            sysUser = sysUserService.getOne(queryWrapper);
        }

        // SysUser sysUser = systemService.getSysUserById(userId).getData();
        queryInfo.setCreateId(sysUser.getUserId());

        List<QueryInfo> list = this.list(new QueryWrapper<>(queryInfo));
        for (QueryInfo info : list) {
            List<QueryDetail> queryDetailList = queryDetailService.list(new QueryWrapper<QueryDetail>().eq("query_id", info.getId()));
            info.setMultiConditionList(queryDetailList);
        }
        return list;
    }

    @Override
    public void deleteQueryInfo(String id) {
        this.removeById(id);
        queryDetailService.remove(new QueryWrapper<QueryDetail>().eq("query_id", id));
    }

    @Override
    public QueryInfo getQueryInfoById(String id) {
        QueryInfo queryInfo = this.getById(id);
        List<QueryDetail> queryDetailList = queryDetailService.list(new QueryWrapper<QueryDetail>().eq("query_id", id));
        queryInfo.setMultiConditionList(queryDetailList);
        return queryInfo;
    }
}
